package cn.com.demo.hana;

import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class HANAJdbcTest {
    private static final String queryString = "SELECT * FROM tables LIMIT 10 offset 20";
    public static void main(String[] argv) {
        Connection connection = null;
        try {
            connection = DriverManager.getConnection(
                    "jdbc:sap://172.30.87.2:30059/?autocommit=false", "SAP_TENC",
                    "sapSAP123");
        } catch ( SQLException e) {
            System.err.println("Connection Failed:");
            System.err.println(e);
            return;
        }
        List<HashMap<String,Object>> datas = new ArrayList<>();
        if (connection != null) {
            try {
                System.out.println("Connection to HANA successful!");
                Statement stmt = connection.createStatement();
                ResultSet resultSet = stmt.executeQuery(queryString);
                ResultSetMetaData metaData = resultSet.getMetaData();
                int columnCount = metaData.getColumnCount();
                while (resultSet.next()) {
                    HashMap<String,Object> data = new HashMap<>();
                    for (int i = 0; i < columnCount; i++) {
                        data.put(metaData.getColumnName(i+1),resultSet.getObject(i+1));
                    }
                    datas.add(data);
                }
                System.out.println("查询结果数："+datas.size());
                System.out.println(datas);
            } catch (SQLException e) {
                System.err.println("Query failed!");
            }
        }
    }
}
